﻿using BCCommon;
using BCEntity.TMS.Packing.PackingCategoryConfig;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data;

namespace BCData.TMS.Packing.PackingCategoryConfig
{
    public class PackingCategoryConfigData : IPackingCategoryConfigData
    {
        private readonly IDatabaseContext database;

        public PackingCategoryConfigData(IDatabaseContext database)
        {
            this.database = database;
        }

        private readonly string QUERY = @"SELECT
		                                PackingCategoryConfigId,
		                                PackingCategory,
		                                CreateAdminId,
		                                CreateAdminName,
		                                CreateTime,
		                                UpdateTime,
		                                EditAdminId,
		                                EditAdminName 
                                    FROM
                                        PackingCategoryConfig";

        public async Task<PackingCategoryConfigEntity> GetByPackingCategory(string packingCategory)
        {
            return await database.Slave.QueryEntityAsync<PackingCategoryConfigEntity>($"{QUERY} WHERE PackingCategory = ?PackingCategory", new { packingCategory });
        }

        public async Task<PackingCategoryConfigEntity> Insert(PackingCategoryConfigEntity entity)
        {
            return await database.Master.InsertAsync(entity);
        }

        public async Task<PackingCategoryConfigEntity> GetEntity(int packingCategoryConfigId)
        {
            return await database.Slave.QueryEntityAsync<PackingCategoryConfigEntity>($"{QUERY} WHERE PackingCategoryConfigId = ?PackingCategoryConfigId", new { packingCategoryConfigId });
        }

        public async Task<IEnumerable<PackingCategoryConfigEntity>> GetList()
        {
            return await database.Slave.QueryAsync<PackingCategoryConfigEntity>(QUERY);
        }

        public async Task<PackingCategoryConfigEntity> Update(PackingCategoryConfigEntity entity)
        {
            return await database.Master.UpdateAsync(entity);
        }

        public async Task<int> Delete(int packingCategoryConfigId)
        {
            return await database.Master.ExecuteAsync("DELETE FROM PackingCategoryConfig WHERE PackingCategoryConfigId = ?PackingCategoryConfigId", new { packingCategoryConfigId });
        }

        public async Task<(IEnumerable<PackingCategoryConfigEntity>, int)> Query(string packingCategory, DateTime? createStartDate, DateTime? createEndDate, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(packingCategory))
            {
                wheres.Add("PackingCategory LIKE ?PackingCategory");
                parameters.Add("?PackingCategory", $"%{packingCategory}%", DbType.AnsiString);
            }
            if (createStartDate.HasValue)
            {
                wheres.Add("CreateTime >= ?CreateStartDate");
                parameters.Add("?CreateStartDate", createStartDate.Value.Date, DbType.Date);
            }
            if (createEndDate.HasValue)
            {
                wheres.Add("CreateTime < ?CreateEndDate");
                parameters.Add("?CreateEndDate", createEndDate.Value.AddDays(1).Date, DbType.Date);
            }
            string sql = string.Format(@"{0} {1} ORDER BY PackingCategoryConfigId DESC LIMIT {2},{3};
                                        SELECT COUNT(1) FROM PackingCategoryConfig {1}",
                                        QUERY,
                                        wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty,
                                        (pageIndex - 1) * pageSize,
                                        pageSize);
            return await database.Slave.QueryPageableListAsync<PackingCategoryConfigEntity>(sql, parameters);
        }
    }
}
